/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.NetworkReportGenerator;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Collection;
import java.sql.Connection;

import java.util.HashMap;
import java.util.HashSet;


/**
 *
 * @author aezhov
 */
public class Switch implements Comparator<String> {
    String name = null;
    String dir = null;
    String portpattern = null;
    Pattern pattern = Pattern.compile("^(.*\\D+)(\\d+)$");
    TreeSet<String> portSet = null;
    Connection dbConnect = null;
    String OpenNMSid = null;
    String adminip = null;
    String snmpVersion = null;
    String snmpCommunity = null;
    String snmpDump = null;
    HashMap<String, HashSet<String>> portToMac = null;
    String macMarker = null;
    boolean isRoot = false;

    public Switch(String name) {
        this.name = name;
    }

    public String toString() {
        return name;
    }

    public void addConfig(String parameter, String[] confStr) {
        if (parameter.equals("dir")) {
            dir = confStr[2];
        } else if (parameter.equals("portpattern")) {
            portpattern = confStr[2];
        } else if (parameter.equals("adminip")) {
            adminip = confStr[2] + "." + confStr[3] + "." + confStr[4] + "." + confStr[5];
            System.out.println("Switch " + name + " admin ip == " + adminip);
        } else if (parameter.equals("snmp")) {
            if (confStr[2].equals("version")) {
                snmpVersion = confStr[3];
            } else if (confStr[2].equals("community")) {
                snmpCommunity = confStr[3];
            } else if (confStr[2].equals("dump")) {
                snmpDump = confStr[3] + "." + confStr[4];
            }
        } else {
            System.out.println("SWITCH " + name + " unknow configuration options");
        }
    }

    public void commitConfig(Connection dbConnect)
            throws ConfigurationException, java.io.IOException
    {
        this.dbConnect = dbConnect;
        
        System.out.println("Commiting configuration for switch " + name);
        if (name == null || name.length() == 0)
            throw new ConfigurationException();

        if (dir == null || portpattern == null)
            throw new ConfigurationException();

        File swDir = new File(dir);
        if (!swDir.exists())
            throw new ConfigurationException();

        // Obtaning OpenNMS id of the switch
        String canonPath = swDir.getCanonicalPath();
        String[] tokens = canonPath.split("/");
        OpenNMSid = tokens[tokens.length - 1];
        
        // Obaining iformation about ports of the switch
        String[] files = swDir.list();
        portSet = new TreeSet<String>(this);
        for (int i = 0; i < files.length; i++) {
            String file = files[i];
            if (file.matches(portpattern)) {
                portSet.add(file);
            }
        }
    }

    TreeSet<String> getPortDirectories() {
        return portSet;
    }

    public boolean isRoot(String monitorStationMAC) {
        Collection<HashSet<String>> mc = portToMac.values();
        Iterator<HashSet<String>> mcIt = mc.iterator();
        while (mcIt.hasNext()) {
            HashSet<String> macs = mcIt.next();
            if (macs.size() == 1 && macs.contains(monitorStationMAC)) {
                System.out.println("ROOT SWITCH " + name);
                isRoot = true;
            }
        }

        return isRoot;
    }

    // Comparator implementation
    public int compare(String o1, String o2) {
       Matcher match1 = pattern.matcher(o1);
       Matcher match2 = pattern.matcher(o2);
       String num1 = null;
       String num2 = null;
       String prefix1 = null;
       String prefix2 = null;

       if (match1.matches()) {
            prefix1 = match1.group(1);
            num1 = match1.group(2);
       }

       if (match2.matches()) {
            prefix2 = match2.group(1);
            num2 = match2.group(2);
       }

       int val1 = Integer.parseInt(num1);
       int val2 = Integer.parseInt(num2);

       if (prefix1.equals(prefix2))
            return val1 - val2;
       else
            return prefix1.compareTo(prefix2);
    }

    public boolean equals(Object o) {
        return true;
    }
}
